package com.doubletick.proxy.ipidea.controller;

import com.doubletick.common.annotation.Log;
import com.doubletick.common.core.controller.BaseController;
import com.doubletick.common.core.domain.AjaxResult;
import com.doubletick.common.core.page.TableDataInfo;
import com.doubletick.common.enums.BusinessType;
import com.doubletick.common.utils.poi.ExcelUtil;
import com.doubletick.proxy.ipidea.domain.ProxyAccount;
import com.doubletick.proxy.ipidea.manager.IProxyAccountManager;
import com.doubletick.proxy.ipidea.service.IProxyAccountService;
import com.doubletick.proxy.ipidea.util.FlowChargeDto;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 代理账号Controller
 *
 * @author ruoyi
 * @date 2024-07-08
 */
@RestController
@RequestMapping("/proxy/account")
public class ProxyAccountController extends BaseController {

    @Resource
    private IProxyAccountService proxyAccountService;

    @Resource
    private IProxyAccountManager proxyAccountManager;

    /**
     * 查询代理账号列表
     */
    @PreAuthorize("@ss.hasPermi('proxy:account:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProxyAccount proxyAccount) {
        startPage();
        List<ProxyAccount> list = proxyAccountService.selectProxyAccountList(proxyAccount);
        return getDataTable(list);
    }

    /**
     * 导出代理账号列表
     */
    @PreAuthorize("@ss.hasPermi('proxy:account:export')")
    @Log(title = "代理账号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProxyAccount proxyAccount) {
        List<ProxyAccount> list = proxyAccountService.selectProxyAccountList(proxyAccount);
        ExcelUtil<ProxyAccount> util = new ExcelUtil<ProxyAccount>(ProxyAccount.class);
        util.exportExcel(response, list, "代理账号数据");
    }

    /**
     * 获取代理账号详细信息
     */
    @PreAuthorize("@ss.hasPermi('proxy:account:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(proxyAccountService.selectProxyAccountById(id));
    }

    /**
     * 新增代理账号
     */
    @PreAuthorize("@ss.hasPermi('proxy:account:add')")
    @Log(title = "代理账号", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProxyAccount proxyAccount) {
        return proxyAccountManager.addAccount(proxyAccount.getAccount(), proxyAccount.getPassword(), proxyAccount.getLimitFlow(), proxyAccount.getRemark());
    }


    /**
     * 修改代理账号
     */
    @PreAuthorize("@ss.hasPermi('proxy:account:edit')")
    @Log(title = "代理账号", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProxyAccount proxyAccount) {
        return proxyAccountManager.update(proxyAccount);

    }

    /**
     * 删除代理账号
     */
    @PreAuthorize("@ss.hasPermi('proxy:account:remove')")
    @Log(title = "代理账号", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
//        return toAjax(proxyAccountService.deleteProxyAccountByIds(ids));
        return AjaxResult.error("不支持的操作");
    }


    @PreAuthorize("@ss.hasPermi('proxy:account:charge_flow')")
    @Log(title = "代理账号充值", businessType = BusinessType.UPDATE)
    @PostMapping("/chargeFlow")
    public AjaxResult chargeFlow(@RequestBody FlowChargeDto chargeFlow) {
        return proxyAccountManager.chargeFlow(chargeFlow);
    }


    @PreAuthorize("@ss.hasPermi('proxy:account:reduce_flow')")
    @Log(title = "代理账号扣除", businessType = BusinessType.UPDATE)
    @PostMapping("/reduceFlow")
    public AjaxResult reduceFlow(@RequestBody FlowChargeDto reduceFlow) {
        return proxyAccountManager.reduceFlow(reduceFlow);
    }

    @PostMapping("/proxyAccountList")
    @PreAuthorize("@ss.hasPermi('proxy:account:list')")
    public AjaxResult proxyAccountList(@RequestBody ProxyAccount proxyAccount) {
        return proxyAccountManager.proxyAccountList(proxyAccount);
    }

    @PostMapping("/userList")
    @PreAuthorize("@ss.hasPermi('proxy:account:list')")
    public AjaxResult userList(@RequestBody ProxyAccount proxyAccount) {
        return proxyAccountManager.userList(proxyAccount);
    }

    @PostMapping("/flowCount")
    @PreAuthorize("@ss.hasPermi('proxy:flow:listByDay')")
    public AjaxResult flowCount() {
        return proxyAccountManager.flowCount();
    }

}
